﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem
{
    public partial class OrgManageWindow : ChildWindow
    {
        SystemService.SystemServiceClient Client = new SystemService.SystemServiceClient();
        SystemService.OrgInfo OrgInfoModel = new SystemService.OrgInfo();


        public int _pid
        {
            get;
            set;
        
        }

        /// <summary>
        /// 新增
        /// </summary>
        public OrgManageWindow(int PID)
        {
            InitializeComponent();
            _pid = PID;
            this.Title = "新增组织机构";
            this.DataContext = OrgInfoModel;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="PID"></param>
        public OrgManageWindow(SystemService.OrgInfo OrgInfoModel1)
        {
            InitializeComponent();
            OrgInfoModel = OrgInfoModel1;
            this.Title = "编辑组织机构";
            this.DataContext = OrgInfoModel;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {

            //编辑
            if (OrgInfoModel.Id > 0)
            {
                Client.UpdateOrgCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_UpdateOrgCompleted);
                Client.UpdateOrgAsync(OrgInfoModel);
            }
            else
            {
                //新增

                //创建时间
                OrgInfoModel.CreateDate = DateTime.Now;
                //父级ID
                OrgInfoModel.PID = _pid;

                Client.InsertOrgCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_InsertOrgCompleted);
                Client.InsertOrgAsync(OrgInfoModel);
            }

            this.DialogResult = true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Client_InsertOrgCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            //throw new NotImplementedException();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Client_UpdateOrgCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

